﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using GaiNo.Core.Services;

namespace GaiNo.Core.Helpers
{
    public class FileHelper
    {
        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="path">绝对路径</param>
        /// <returns></returns>
        public static bool IsFileExist(string path)
        {
            return System.IO.File.Exists(path);
        }
        /// <summary>
        /// 判断路径是否存在
        /// </summary>
        /// <param name="path">绝对路径</param>
        /// <returns></returns>
        public static bool IsDirectoryExist(string path)
        {
            return System.IO.Directory.Exists(path);
        }
        /// <summary>
        /// 删除存在的文件，成功返回true，失败或文件不存在返回false
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool DeleteExistFile(string path)
        {
            if (!IsFileExist(path))
            {
                return false;
            }
            try
            {
                System.IO.File.Delete(path);
                return true;
            }
            catch (System.IO.IOException ex)
            {
                Log.Current.ApplicationError("DeleteExistFile", ex.ToString());
                return false;
            }
        }
        public static bool CreateDirectory(string path)
        {
            if (IsDirectoryExist(path))
                return false;

            try
            {
                System.IO.Directory.CreateDirectory(path);
                return true;
            }
            catch (System.IO.IOException ex)
            {
                Log.Current.ApplicationError("CreateDirectory", ex.ToString());
                return false;
            }
        }
        public static string GetSavePath(string prefix)
        {
            DateTime now = DateTime.Now;
            string result = prefix + "/" + now.ToString("yy-MM") + "/" + now.ToString("dd") + "/";
            return result;
        }
        public static string GetExtension(string path)
        {
            return Path.GetExtension(path).ToUpper();
        }
        private static Encoding defaultEncoding = Encoding.UTF8;
        #region ReadTextFile
        /// <summary>
        /// ReadTextFile
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <returns>文本内容</returns>
        public static string ReadTextFile(string fileName)
        {
            return ReadTextFile(fileName, defaultEncoding);
        }

        /// <summary>
        /// ReadTextFile
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="encoding">编码</param>
        /// <returns>文本内容</returns>
        public static string ReadTextFile(string fileName, Encoding encoding)
        {
            string text;

            using (StreamReader sr = new StreamReader(fileName, encoding))
            {
                text = sr.ReadToEnd();
            }

            return text;
        }
        #endregion

        #region WriteTextFile
        /// <summary>
        /// WriteTextFile
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="text">文本内容</param>
        public static void WriteTextFile(string fileName, string text)
        {
            WriteTextFile(fileName, text, false, true, defaultEncoding);
        }

        /// <summary>
        /// WriteTextFile
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="text">文本内容</param>
        /// <param name="encoding">编码</param>	
        public static void WriteTextFile(string fileName, string text, Encoding encoding)
        {
            WriteTextFile(fileName, text, false, true, encoding);
        }

        /// <summary>
        /// WriteTextFile
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="text">文本内容</param>
        /// <param name="createDir">是否创建目录</param>		
        public static void WriteTextFile(string fileName, string text, bool createDir)
        {
            WriteTextFile(fileName, text, false, createDir, defaultEncoding);
        }

        /// <summary>
        /// WriteTextFile
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="text">文本内容</param>
        /// <param name="createDir">是否创建目录</param>
        /// <param name="encoding">编码</param>	
        public static void WriteTextFile(string fileName, string text, bool createDir, Encoding encoding)
        {
            WriteTextFile(fileName, text, false, createDir, encoding);
        }

        /// <summary>
        /// WriteTextFile
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="text">文本内容</param>
        /// <param name="append">是否添加到文本后面</param>
        /// <param name="createDir">是否创建目录</param>
        /// <param name="encoding">编码</param>
        public static void WriteTextFile(string fileName, string text, bool append, bool createDir, Encoding encoding)
        {
            if (createDir)
            {
                string dirName = Path.GetDirectoryName(fileName);

                if (!Directory.Exists(dirName))
                {
                    Directory.CreateDirectory(dirName);
                }
            }

            using (StreamWriter sw = new StreamWriter(fileName, append, encoding))
            {
                sw.Write(text);
            }
        }
        #endregion

        /// <summary>
        /// ReadFileReturnBytes 从文件中读取二进制数据
        /// </summary>      
        public static byte[] ReadFileReturnBytes(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return null;
            }

            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            BinaryReader br = new BinaryReader(fs);

            byte[] buff = br.ReadBytes((int)fs.Length);

            br.Close();
            fs.Close();

            return buff;
        }
    }
}
